package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.Resource;
import com.coolwen.entity.Role;
import com.coolwen.entity.User;
import com.coolwen.service.RoleService;
import com.coolwen.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on .
 */
@Controller
@RequestMapping("/admin/user/")
public class UserController {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("list")
    public String list(Model model) {
        model.addAttribute("users", userService.list());
        return "user/list";
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("roles", roleService.listRole());
        return "user/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(User user, HttpServletRequest req, Model model) {
        String[] trids = req.getParameterValues("rids");
        List<Long> rids = new ArrayList<Long>();
        for (String rid : trids) {
            rids.add(Long.parseLong(rid));
        }
        userService.add(user, rids);
        return "redirect:/admin/user/list";
    }

    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable int id) {

        //只更新用户状态
        User u = new User();
        User user = userService.load(id);
        u.setLocked(user.getLocked());
        u.setId(user.getId());
        //       logger.info("updateStatus配置Controller:" + JSON.toJSON(u));
        if (u.getLocked() == 0) {
            u.setLocked((byte) 1);
        } else {
            u.setLocked((byte) 0);
        }
        userService.update(u);
        return "redirect:/admin/user/list";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable int id, Model model) {
        User user = userService.load(id);
        //       logger.info("update配置Controller:" + JSON.toJSON(user));
        model.addAttribute("user", user);
        model.addAttribute("roles", roleService.listRole());
        List<Role> hasRoles = userService.loadUserRole(id).getRoles();
        List<Integer> rids = new ArrayList<Integer>();
        for (Role r : hasRoles) {
            rids.add(r.getId().intValue());
        }
        model.addAttribute("hasRole", rids);
        //       logger.info("配置rids:" + JSON.toJSON(hasRoles));
        //       logger.info("配置rids:" + JSON.toJSON(rids));
        return "user/update";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.POST)
    public String update(@PathVariable int id, HttpServletRequest req, User user, Model model) {
//        userService.load(id);
//        logger.info("配置rids:" + JSON.toJSON(user));
        logger.info("update配置U:" + JSON.toJSON(user));
        User tu = userService.load(id);
//        tu.setNickname(user.getNickname());
        tu.setPassword(user.getPassword());
        tu.setUsername(user.getUsername());
 //       tu.setLocked(user.getLocked());
        String[] trids = req.getParameterValues("rids");
        List<Integer> rids = new ArrayList<Integer>();
        for (String rid : trids) {
            rids.add(Integer.parseInt(rid));
        }
        logger.info("update配置TU:" + JSON.toJSON(tu));
        userService.update(tu, rids);
        return "redirect:/admin/user/list";
    }

    @RequestMapping("/listRes/{id}")
    public String listRes(Model model, @PathVariable long id) {
        List<Resource> hasRes = userService.listUserRes(id);
        model.addAttribute("reses", hasRes);
        model.addAttribute("user", userService.load(id));
        return "user/res";
    }

    /**
     * 删除用户
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
    public String deleteByPrimaryKey(@PathVariable int id, HttpServletRequest req, User user, Model model){
//        User tu = userService.load(id);
//        tu.setPassword(user.getPassword());
//        tu.setUsername(user.getUsername());
//        String[] trids = req.getParameterValues("rids");
//        List<Integer> rids = new ArrayList<Integer>();
//        for (String rid : trids) {
//            rids.add(Integer.parseInt(rid));
//        }
//        userService.update(tu, rids);
        userService.delete(id);
        return "redirect:/admin/user/list";
    }
}
